package com.zmn.oms.dubbo.impl.zmn.normal.track;

import org.apache.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.zmn.common.dto.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.reflect.CopyObjectUtil;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.oms.dubbo.dto.common.track.AddMasterTrackDIO;
import com.zmn.oms.dubbo.dto.common.track.OrderTrackQueryDIO;
import com.zmn.oms.dubbo.dto.common.work.WorkAddTrackDIO;
import com.zmn.oms.dubbo.interfaces.zmn.normal.track.ZsNormalOrderTrackRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.track.AddOrderTrackDTO;
import com.zmn.oms.model.dto.track.OrderTrackDTO;
import com.zmn.oms.model.dto.track.OrderTrackDetailDTO;
import com.zmn.oms.model.dto.track.WorkTrackDTO;
import com.zmn.oms.model.entity.track.OrderTrackQuery;
import com.zmn.oms.zmn.normal.business.interfaces.track.ZsNormalOrderTrackBService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 类描述：工程师跟单
 *
 * @author heciqi
 * @date 2018/11/27 14:40
 */
@Service(loadbalance = "roundrobin", timeout = 5000, retries = 2, version = DubboConsts.INTERFACE_VERSION, validation = "false")
public class ZsNormalOrderTrackRemoteServiceImpl implements ZsNormalOrderTrackRemoteService {


    private static final String TAG = "啄木鸟-普通工单-工程师跟单dubbo接口";
    private Logger logger = LoggerFactory.getLogger(ZsNormalOrderTrackRemoteServiceImpl.class);
    @Autowired
    private ZsNormalOrderTrackBService zsNormalOrderTrackBService;

    /**
     * 返回数量
     *
     * @param orderTrackQueryDIO
     * @return
     */
    @Override
    public ResponseDTO<Integer> countByQuery(OrderTrackQueryDIO orderTrackQueryDIO) {
        OrderTrackQuery orderTrackQuery = new OrderTrackQuery();
        CopyObjectUtil<OrderTrackQueryDIO, OrderTrackQuery> copyObjectUtil = new CopyObjectUtil<>(orderTrackQueryDIO, orderTrackQuery);
        copyObjectUtil.copy();

        Integer integer = zsNormalOrderTrackBService.countByQuery(orderTrackQuery);

        return ResponseUtil.responseSuccess(integer, "OK");
    }

    /**
     * 新增工程师跟单
     *
     * @param addMasterTrackDIO
     * @return
     */
    @Override
    public ResponseDTO insertMasterTrack(AddMasterTrackDIO addMasterTrackDIO) {
        AddOrderTrackDTO destDTO = new AddOrderTrackDTO();
        CopyObjectUtil<AddMasterTrackDIO, AddOrderTrackDTO> copyObjectUtil = new CopyObjectUtil<>(addMasterTrackDIO, destDTO);
        copyObjectUtil.copy();
        destDTO.setAddUserId(addMasterTrackDIO.getMasterId());
        zsNormalOrderTrackBService.insertMasterTrack(destDTO);

        return ResponseUtil.responseSuccess(null, "OK");
    }

    @Override
    public com.zmn.common.dto2.ResponseDTO modifyWorkTrack(WorkAddTrackDIO workAddTrackDIO) {
        try {
            logger.info("跟单系统新增oms跟单入参[{}]", JSON.toJSONString(workAddTrackDIO));
            WorkTrackDTO workTrackDTO = BeanMapper.map(workAddTrackDIO, WorkTrackDTO.class);
            zsNormalOrderTrackBService.modifyOrderWorkStatus(workTrackDTO);
        } catch (Exception e){
            e.printStackTrace();
            logger.info("跟单系统新增oms跟单失败：[{}]", e.getMessage());
            return com.zmn.common.dto2.ResponseDTO.fail(e.getMessage());
        }
        return com.zmn.common.dto2.ResponseDTO.success();
    }
}
